package com.augurit.gzcgwsp.common.util;

import java.rmi.RemoteException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
import org.apache.log4j.Logger;

import com.augurit.common.base.schedule.JdkExecutorJobTask;

import sun.misc.BASE64Encoder;

/**
 * 客户端调用类
 * @version 1.0 
 * @since 2011-5-11
 */
public class WSClient {
	private static Logger log=Logger.getLogger(WSClient.class);
    /**
     * 调用方法
     * @param wsdl  wsld地址(OA提供)
     * @param filePath 文件路径(绝对路径)
     * @param title  标题
     * @param creator 拟稿人
     * @return 交换返回信息：success（表示成功)
     * @throws Exception
     */
    public static String  invoke(String filePath,String title,String creator) throws Exception{
	    String fbresult="";
            try {
            	Locale locale = Locale.getDefault(); 
    			ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
    			String wsdl = localResource.getString("OAWSDL");
    			log.info("oa地址"+wsdl);
    			//wsdl="http://192.168.12.241:7001/applyservice/services/WService?wsdl";//测试地址
                BASE64Encoder encoder = new BASE64Encoder();  
                DataHandler dh = new DataHandler(new FileDataSource(filePath));   
                Service service = new Service();
                Call call = (Call)service.createCall();
                call.setTargetEndpointAddress(new java.net.URL(wsdl));
                call.setOperationName(new QName(wsdl, "getApprovalInfo"));
                QName qn = new QName("DataHandler", "myns:DataHandler");   
                call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class);   
                call.addParameter("file", qn, ParameterMode.IN);   
                call.addParameter("title", XMLType.XSD_STRING, ParameterMode.IN);   
                call.addParameter("creator", XMLType.XSD_STRING, ParameterMode.IN);   
                call.setReturnClass(String.class);   
                fbresult = (String)call.invoke(new Object[]{dh, encoder.encode(title.getBytes("GBK")),encoder.encode(creator.getBytes("GBK"))});   
            } catch (RemoteException e) {
            	e.printStackTrace();
               log.error("连接服务器失败，请检查WSDL地址是否正确或联系OA管理员!"+e);
            } catch (Exception e) {
            	log.error("交换异常，请联系管理员!"+e.getMessage());
            } 
        
        return fbresult;
	}
    public static void main(String[] args) throws Exception{
    	WSClient client=new WSClient();
    	String vid=client.invoke("F:\\gzcgw1220\\gzcgwsp\\gzcgwsp\\WebApp\\printdoc\\rqjyxkz_pzqy\\1325139528360.doc", "测试文件发送", "gonglinglin");
    	System.out.print(vid);
    }
}
